<?php

namespace App\Admin\Controllers;

use App\FeeActivity;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class FeeActivityController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '服务费优惠折扣';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new FeeActivity());

        $grid->header(function ($query) {
            $fee = DB::table('config')->where('key', 'fee')->first();
            return "当前服务费：{$fee->value}";
        });

        $grid->column('id', __('Id'));
        $grid->column('discount', '折扣');
        $grid->column('start', '开始时间');
        $grid->column('end', '结束时间');
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(FeeActivity::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('discount', '折扣');
        $show->field('start', '开始时间');
        $show->field('end', '结束时间');
        $show->field('created_at', '创建时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new FeeActivity());

        $form->text('discount', '折扣');
        $form->datetime('start', '开始时间')->default(date('Y-m-d H:i:s'));
        $form->datetime('end', '结束时间')->default(date('Y-m-d H:i:s'));

        return $form;
    }
}
